//
//  CSinaWeibo.m
//  Delicious
//
//  Created by yilong zhang on 11-11-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CSinaWeibo.h"

@implementation CSinaWeibo

@synthesize m_SinaWeiboDelegate;
@synthesize m_Engine;
@synthesize m_WeiboClient;
@synthesize m_strUserName;
@synthesize m_strTokenKey;
@synthesize m_strSecret;
@synthesize m_strRequestKey;
@synthesize m_PresentView;

static CSinaWeibo  *g_Instance = nil;          //全局

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if (nil == g_Instance)
        {
            g_Instance = [super allocWithZone:zone];
        }
    }
    
    return g_Instance;
}

+(CSinaWeibo*)DefaultSinawb
{
    if (nil == g_Instance)
    {
        g_Instance = [[CSinaWeibo alloc] init];
    }
    
    return g_Instance;
}

+(void)DestroySinawb
{
    [g_Instance release];
    g_Instance = nil;
}

- (id)init
{
    self = [super init];
    if (self) 
    {
        // Initialization code here.
        m_Engine = [[OAuthEngine alloc] initOAuthWithDelegate: self];
		m_Engine.consumerKey    = Sina_AppKey;
		m_Engine.consumerSecret = Sina_AppSecret;
        
        m_WeiboClient = nil;
    }
    
    return self;
}

-(void)dealloc
{
    [m_strTokenKey release];
    [m_Engine release];
    [m_strUserName release];
    
    [super dealloc];
}

- (NSString*)loadDefaultKey
{
    self.m_strRequestKey = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"sina_%@", m_strUserName]];
	self.m_strTokenKey   = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"sina_key_%@", m_strUserName]];
    self.m_strSecret     = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"sina_secret_%@", m_strUserName]];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return self.m_strRequestKey;
}

- (void)saveDefaultKey
{
    NSDictionary *params = [NSURL parseURLQueryString:self.m_strRequestKey];
    self.m_strTokenKey = [params objectForKey:@"oauth_token"];
    self.m_strSecret   = [params objectForKey:@"oauth_token_secret"];
    
    [[NSUserDefaults standardUserDefaults] setValue:self.m_strRequestKey forKey:[NSString stringWithFormat:@"sina_%@", m_strUserName]];
    [[NSUserDefaults standardUserDefaults] setValue:self.m_strTokenKey forKey:[NSString stringWithFormat:@"sina_key_%@", m_strUserName]];
    [[NSUserDefaults standardUserDefaults] setValue:self.m_strSecret forKey:[NSString stringWithFormat:@"sina_secret_%@", m_strUserName]];
	[[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)storeCachedOAuthData:(NSString*)data forUsername:(NSString *)username 
{
    self.m_strRequestKey = data;

    [self saveDefaultKey];
    
    /*[OAuthEngine setCurrentOAuthEngine:m_Engine];
    
   m_WeiboClient = [[WeiboClient alloc] initWithTarget:self 
                                                 engine:m_Engine
                                                 action:@selector(DidReceiveSinaData:obj:)];*/
    
    //[self LoginSinaWeiboSuc];
}

- (NSString *) cachedOAuthDataForUsername:(NSString *)username 
{
    return [self loadDefaultKey];
}

- (void)removeCachedOAuthDataForUsername:(NSString *) username
{
	[self LogoutSinaWeibo];
}

-(void)LoginSinaWeibo:(UIViewController*)incontroller Name:(NSString*)name
{
    self.m_strUserName = name;
    
    UIViewController *controller = nil;
    controller = [OAuthController controllerToEnterCredentialsWithEngine: m_Engine delegate: self];
    controller.view.frame = CGRectMake(0, 0, incontroller.view.frame.size.width, incontroller.view.frame.size.height);
    
    if (controller) 
    {
        [incontroller presentModalViewController: controller animated: YES];
    }
	else
    {
		[OAuthEngine setCurrentOAuthEngine:m_Engine];
        
        m_WeiboClient = [[WeiboClient alloc] initWithTarget:self 
                                                     engine:m_Engine
                                                     action:@selector(DidReceiveSinaData:obj:)];
        [self LoginSinaWeiboSuc];
    }
    
    m_PresentView = incontroller;
}

-(void)LoginSinaWeiboSuc
{
    [m_SinaWeiboDelegate DidLoginSinaWeibo:self State:0];
}

-(void)LogoutSinaWeibo
{
    //[[NSUserDefaults standardUserDefaults] removeObjectForKey:sina_AppTokenKey];
    //[[NSUserDefaults standardUserDefaults] synchronize];
    [m_Engine signOut];
    m_strUserName = nil;
    m_strTokenKey = nil;
}

-(void)ShareToSinaWeibo:(NSString*)content ImageFile:(UIImage*)file
{    
    NSData  *data  = UIImageJPEGRepresentation(file, 1);
    [m_WeiboClient upload:data status:content];
}

- (void) OAuthController: (OAuthController *) controller authenticatedWithUsername: (NSString *) username
{
    [OAuthEngine setCurrentOAuthEngine:m_Engine];
    
    m_WeiboClient = [[WeiboClient alloc] initWithTarget:self 
                                                 engine:m_Engine
                                                 action:@selector(DidReceiveSinaData:obj:)];
    
    [self LoginSinaWeiboSuc];
}

- (void)DidReceiveSinaData:(WeiboClient*)sender obj:(NSObject*)obj
{
    NSDictionary *dic = (NSDictionary*)obj;
    
    if (nil != [dic objectForKey:@"thumbnail_pic"])
    {
        NSLog(@"share to sina successed!");
    }
    else
    {
        NSLog(@"share to sian failed!");
    }
}

- (void) OAuthControllerFailed: (OAuthController *) controller
{
    NSLog(@"OAuthControllerFailed!");
    [m_SinaWeiboDelegate DidLoginSinaWeibo:self State:1];
}

- (void) OAuthControllerCanceled: (OAuthController *) controller
{
    NSLog(@"OAuthControllerCanceled!");
    [m_SinaWeiboDelegate DidLoginSinaWeibo:self State:1];
}

@end
